package com.yodawnla.simFish;

import java.util.ArrayList;

import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.yodawnla.lib.util.YoVector2;
import com.yodawnla.lib.util.tool.YoTimer;
import com.yodawnla.simFish.scene.GameScene;

public class Fish extends Sprite
{
	GameScene mScene;
	int mType = 0;
	YoVector2 mVector = new YoVector2();
	
	
	public Fish(float pX, float pY, TextureRegion pTextureRegion, GameScene scene, int type)
	{
		super(pX, pY, pTextureRegion);
		mType = type;
		mScene = scene;
		
		YoTimer updateTimer = new YoTimer(0.5f)
		{
			@Override
			public void onTimePassed()
			{
				mScene.getBaseActivity().runOnUpdateThread(new Runnable()
				{
					public void run()
					{
						if(!Fish.this.isVisible())
							return;
						
						ArrayList<Fish> lFishList = mScene.getLFishList();
						
						switch(mType)
						{
						case Values.FishType.S_FISH:
							ArrayList<Fish> mFishList = mScene.getMFishList();
							for(int i = 0; i < mFishList.size(); i++)
							{
								if(!Fish.this.isVisible())
									break;
								
								if(Fish.this.collidesWith(mFishList.get(i)))
									Fish.this.setVisible(false);	
							}
//							for(int i = 0; i < lFishList.size(); i++)
//							{
//								if(!Fish.this.isVisible())
//									break;
//								
//								if(Fish.this.collidesWith(lFishList.get(i)))
//									Fish.this.setVisible(false);
//							}
							ArrayList<Sprite> mFeedList = mScene.getFeedList();
							for(int i = 0; i < mFeedList.size(); i++)
							{
								if(!Fish.this.isVisible())
									break;
								
								Sprite feed = mFeedList.get(i);
								if(feed.isVisible() && Fish.this.collidesWith(feed))
								{
									feed.setScale(feed.getScaleX() - 0.1f);
									if(feed.getScaleX() <= 0.5f)
									{
										feed.setScale(3);
										feed.setVisible(false);
									}
								}
							}
							break;
						case Values.FishType.M_FISH:
							for(int i = 0; i < lFishList.size(); i++)
							{
								if(!Fish.this.isVisible())
									break;
								
								if(Fish.this.collidesWith(lFishList.get(i)))
									Fish.this.setVisible(false);
							}
							break;
						case Values.FishType.L_FISH:
							break;
						}
					}
				});
			}
		};
		updateTimer.start();
	}
	
	public YoVector2 getVector()
	{
		return mVector;
	}
	
	public int getType()
	{
		return mType;
	}
}